#ifndef PROJECT_NAVIGATOR_H
#define PROJECT_NAVIGATOR_H

#include <idocument.h>
#include <ieditor.h>
#include <mimetype.h>

#include <QtCore/QHash>
#include <QtGui/QTreeWidget>

QT_BEGIN_NAMESPACE
class QMenu;
class QToolBar;
QT_END_NAMESPACE

namespace swp
{

class ProjectNavigator : public QTreeWidget, public core::IEditor
{
    Q_OBJECT

public:
    ProjectNavigator(const QList<QAction* >& projectDependentActions,
        QWidget* parent = 0);
    ~ProjectNavigator();

    QSharedPointer<core::IDocument> document() const;
    QWidget* widget() const;

    QStringList galleryGroups() const;
    QList<core::GalleryItem> galleryItems(const QString& group) const;

    void registerFocusListener(QObject* object);
    void unregisterFocusListener(QObject* object);

    QSharedPointer<core::IContainerDocument> selectedProject() const;

    bool event(QEvent *event);

    void addWindowMenuActions();
    void addToolBarActions(QToolBar* toolbar);

    QList<QAction* > perspectiveEnsuringActions() const;

protected:
    // IEditor
    IEditor* copy() const;

    void contextMenuEvent(QContextMenuEvent* event);
    void startDrag(Qt::DropActions supportedActions);
    void dragMoveEvent(QDragMoveEvent* event);
    void dropEvent(QDropEvent* event);

private:
    void createActions();
    void connectSlots();
    void finishDrag();

signals:
    void aboutToAdd(const QSharedPointer<core::IDocument> project,
        const QList<core::MimeType> possibilities, uint requireLinks);
    void aboutToRemove(const QSharedPointer<core::IDocument> project,
        const QSharedPointer<core::IDocument> document);
    void aboutToDelete(const QSharedPointer<core::IDocument> document);
    void aboutToOpen(const QSharedPointer<core::IDocument> document);
    void aboutToSave(const QSharedPointer<core::IDocument> document);
    void aboutToClose(const QSharedPointer<core::IDocument> document);
    void aboutToMove(const QSharedPointer<core::IDocument> project,
        const QSharedPointer<core::IDocument> document);
    void selectionDeferred(QTreeWidgetItem* item);

public slots:
    // IEditor
    void open(const QSharedPointer<core::IDocument>& document);
    void add(const QSharedPointer<core::IDocument>& project,
        const QSharedPointer<core::IDocument>& document);
    void remove(const QSharedPointer<core::IDocument>& project,
        const QSharedPointer<core::IDocument>& document);
    void remove(const QSharedPointer<core::IDocument>& document);
    void close(const QSharedPointer<core::IDocument>& document);

    void tryToSelectProject(
        const QSharedPointer<core::IContainerDocument>& project);
    void tryToSelectDocument(
        const QSharedPointer<core::IDocument>& document);

    void closeSelected();
    void renameSelected();
    void closeSelectedProject();
    void saveSelectedProject();

private slots:
    void addDocument();
    void removeDocument();
    void deleteDocument();
    void openDocument(QTreeWidgetItem* item, int column);
    void ensureDataConsistency(QTreeWidgetItem* item, int column);
    void ensureDocumentNameConsistency();
    void selectItem(QTreeWidgetItem* item);
    void setLinks();

private:
    bool m_inDrag;
    bool m_draggedIsContainer;
    QTreeWidgetItem* m_draggedItem;
    QHash<QTreeWidgetItem*, QSharedPointer<core::IDocument> >
        m_itemToDocumentMapping;
    QList<QAction* > m_openedProjectDependentActions;
    QMenu* m_contextMenu;

    QAction* m_closeAction;
    QAction* m_renameAction;
    QAction* m_addDocumentAction;
    QAction* m_addDocumentMenuAction;
    QAction* m_removeAction;
    QAction* m_deleteAction;

    QAction* m_collapseAll;
    QAction* m_expandAll;

    QAction* m_setLinks;
};

} // namespace swp

#endif
